﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ThoiTrangT2B
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        private static int pID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["pID"] != null)
            {
                pID = Convert.ToInt32(Request.QueryString["pID"]);
                LoadSoLuongSanPham(pID);

            }
            if (!IsPostBack)
            {
                LoadSanPham(pID);
                LoadColor();
                LoadSize();
            }
        }

        private void LoadSoLuongSanPham(int productID)
        {
            string where = "";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, productID));
            if (rbtSize.SelectedItem != null)
            {
                where += "AND k.SizeID_DM = @SizeID_DM ";
                arrParams.Add(SqlHelper.CreateParameterObject("@SizeID_DM", SqlDbType.Int, ParameterDirection.Input, rbtSize.SelectedValue));
            }
            if (chkMauSac.SelectedItem != null)
            {
                where += "AND k.ColorID_DM = @ColorID_DM ";
                arrParams.Add(SqlHelper.CreateParameterObject("@ColorID_DM", SqlDbType.Int, ParameterDirection.Input, chkMauSac.SelectedValue));
            }
            string sql = "SELECT SUM(k.SL) AS SoLuong FROM dbo.tblT2BSanPham s INNER JOIN dbo.tblT2BMenu m ON s.Menu_ID = m.ID " +
                         "INNER JOIN dbo.tblT2BKhoHang k ON s.ID = k.SanPhamID WHERE  s.ID = @ID " + where + " GROUP BY s.ID";
            object soluong = SqlHelper.ExecuteScalar(CommandType.Text, sql, arrParams);
            if (soluong == null)
            {
                ltrSoLuong.Text = "0";
                //ltrImage.Text = "<a href='" + dt.Rows[0]["Image"] + "' class='jqzoom fancybox' data-fancybox-group='gallery'  rel='gal1' ><img src='" + dt.Rows[0]["Image"] + "' width='300px' style='border: 4px solid #666;' /></a>";
            }
            else
            {
                ltrSoLuong.Text = soluong.ToString(); ;
            }
        }

        private void LoadSize()
        {
            string sql = "SELECT d.ID,d.TenDM FROM dbo.tblT2BDanhMuc d INNER JOIN dbo.tblT2BKhoHang k ON d.ID = k.SizeID_DM WHERE k.SanPhamID = " + pID + " GROUP BY TenDM,d.ID";

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            if (dt.Rows.Count > 0)
            {
                ListItem item;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    item = new ListItem("<span class='size'>" + dt.Rows[i]["TenDM"] + "</span>", dt.Rows[i]["ID"].ToString());
                    rbtSize.Items.Add(item);
                }
            }
        }

        private void LoadColor()
        {
            string sql = "SELECT d.ID,d.TenDM FROM dbo.tblT2BDanhMuc d INNER JOIN dbo.tblT2BKhoHang k ON d.ID = k.ColorID_DM WHERE k.SanPhamID = " + pID + " GROUP BY TenDM,d.ID";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            if (dt.Rows.Count > 0)
            {
                ListItem item;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    item = new ListItem("<div class='color-tag' style='background-color: " + dt.Rows[i]["TenDM"] + "'></div>", dt.Rows[i]["ID"].ToString());
                    chkMauSac.Items.Add(item);
                }
            }
        }

        private void LoadSanPham(int productID)
        {
            string sql = "SELECT s.ID,s.ThuongHieuID_DM,s.TenSP,s.MaSP,s.Image,SUM(k.SL) AS SoLuong,s.Gia,s.KM,m.Ten,m.ID AS MenuID " +
                         "FROM dbo.tblT2BSanPham s INNER JOIN dbo.tblT2BMenu m ON s.Menu_ID = m.ID " +
                         "LEFT JOIN dbo.tblT2BKhoHang k ON s.ID = k.SanPhamID WHERE  s.ID = @ID " +
                         " GROUP BY s.ID,s.ThuongHieuID_DM,s.TenSP,s.Image,s.Gia,s.KM,m.Ten,m.ID,s.MaSP";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, productID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            ltrImage.Text = "<a href='" + dt.Rows[0]["Image"] + "' class='jqzoom fancybox' data-fancybox-group='gallery'  rel='gal1' ><img src='" + dt.Rows[0]["Image"] + "' width='300px' style='border: 4px solid #666;' /></a>";
            txtTenSP.Text = dt.Rows[0]["TenSP"].ToString();
            txtMaSP.Text = dt.Rows[0]["MaSP"].ToString();
            int Gia = Convert.ToInt32(dt.Rows[0]["Gia"]);
            int KM = Convert.ToInt32(dt.Rows[0]["KM"]);
            txtGiaSP.Text = (Gia - (Gia * KM / 100)).ToString("###,###");
            txtGiaCu.Text = Gia.ToString("###,###");
            hplCategory.NavigateUrl = "XemSanPham.aspx?cID=" + dt.Rows[0]["MenuID"].ToString();
            hplCategory.Text = dt.Rows[0]["Ten"].ToString();
            ltrSoLuong.Text = dt.Rows[0]["SoLuong"] == DBNull.Value ? "0" : dt.Rows[0]["SoLuong"].ToString();
            ltrHang.Text = SqlHelper.ExecuteScalar(CommandType.Text, "SELECT TenDM FROM dbo.tblT2BSanPham sp INNER JOIN dbo.tblT2BDanhMuc d ON sp.ThuongHieuID_DM = d.ID WHERE sp.ID = " + productID).ToString();
            LoadAnhSanPham(productID);

        }

        private void LoadAnhSanPham(int productID)
        {
            string sql = "SELECT Image FROM tblT2BAnhSanPham WHERE IDSanPham = @ID AND Image is not null";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, productID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            sql = "SELECT Image FROM tblT2BKhoHang WHERE SanPhamID = @ID AND Image is not null GROUP BY Image";
            DataTable dtsmall = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            dt.Merge(dtsmall);
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }

        protected void btnMuaNgay_Click(object sender, EventArgs e)
        {
            if (Session["GioHang"] != null)
            {
                int kt = 0;
                int item = Convert.ToInt32(pID);
                Dictionary<Int32, Int32> lst = (Dictionary<Int32, Int32>)Session["GioHang"];
                for (int i = 0; i < lst.Count; i++)
                {
                    if (item == lst.ElementAt(i).Key)
                    {
                        kt++;
                    }
                }
                if (kt == 0)
                {
                    lst.Add(Convert.ToInt32(pID), 1);
                }
            }
            else
            {
                Dictionary<Int32, Int32> lst = new Dictionary<Int32, Int32>();
                lst.Add(Convert.ToInt32(pID), 1);
                Session["GioHang"] = lst;
            }
        }


    }
}
